using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using CodeWork.DAL.DataContainers.SqlServer;
using System.Data.SqlTypes;
using CodeWork.Library;
using CodeWork.Library.Web;

public partial class admin_SitesettingsEdit : BasePage
{
    private SecurityLevel _securityLevel = SecurityLevel.One;

    protected void Page_Load(object sender, EventArgs e)
    {
        Title = _siteName;

        if (!IsPostBack)
        {
            if (Request["id"] != null)
                LoadData();
        }
    }

    private void LoadData()
    {
        int id = 0;
        Sitesettings sitesettings;

        if (Int32.TryParse(Request["id"], out id))
            sitesettings = Sitesettings.Load(id);
        else
            sitesettings = null;

        if (sitesettings != null)
        {
            if (!string.IsNullOrEmpty(sitesettings.SettingName))
				tbxSettingName.Text = sitesettings.SettingName;
			if (!string.IsNullOrEmpty(sitesettings.AdminEmail))
				tbxAdminEmail.Text = sitesettings.AdminEmail;
			if (!string.IsNullOrEmpty(sitesettings.SecurityKey))
				tbxSecurityKey.Text = sitesettings.SecurityKey;
			if (!string.IsNullOrEmpty(sitesettings.SupportEmail))
				tbxSupportEmail.Text = sitesettings.SupportEmail;
			if (!string.IsNullOrEmpty(sitesettings.SmtpHost))
				tbxSmtpHost.Text = sitesettings.SmtpHost;
			if (!string.IsNullOrEmpty(sitesettings.SmtpUsername))
				tbxSmtpUsername.Text = sitesettings.SmtpUsername;
			if (!string.IsNullOrEmpty(sitesettings.SmtpPassword))
				tbxSmtpPassword.Text = sitesettings.SmtpPassword;
			tbxSmtpPort.Text = sitesettings.SmtpPort.ToString();
			cbxSmtpUseSsl.Checked = sitesettings.SmtpUseSsl;
			if (!string.IsNullOrEmpty(sitesettings.SiteName))
				tbxSiteName.Text = sitesettings.SiteName;
			if (!string.IsNullOrEmpty(sitesettings.DefaultTheme))
				tbxDefaultTheme.Text = sitesettings.DefaultTheme;
			tbxDefaultTimeZone.Text = sitesettings.DefaultTimeZone.ToString();
			tbxDefaultUserroleId.Text = sitesettings.DefaultUserroleId.ToString();
			if (!string.IsNullOrEmpty(sitesettings.CurrentDomain))
				tbxCurrentDomain.Text = sitesettings.CurrentDomain;

            ViewState["id"] = id;
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        string settingName = Security.BindStringInput(tbxSettingName.Text, _securityLevel);
		string adminEmail = Security.BindStringInput(tbxAdminEmail.Text, _securityLevel);
		string securityKey = Security.BindStringInput(tbxSecurityKey.Text, _securityLevel);
		string supportEmail = Security.BindStringInput(tbxSupportEmail.Text, _securityLevel);
		string smtpHost = Security.BindStringInput(tbxSmtpHost.Text, _securityLevel);
		string smtpUsername = Security.BindStringInput(tbxSmtpUsername.Text, _securityLevel);
		string smtpPassword = Security.BindStringInput(tbxSmtpPassword.Text, _securityLevel);
		int smtpPort = Security.BindIntegerInput(tbxSmtpPort.Text, _securityLevel);
		bool smtpUseSsl = cbxSmtpUseSsl.Checked;
		string siteName = Security.BindStringInput(tbxSiteName.Text, _securityLevel);
		string defaultTheme = Security.BindStringInput(tbxDefaultTheme.Text, _securityLevel);
		int defaultTimeZone = Security.BindIntegerInput(tbxDefaultTimeZone.Text, _securityLevel);
		int defaultUserroleId = Security.BindIntegerInput(tbxDefaultUserroleId.Text, _securityLevel);
		string currentDomain = Security.BindStringInput(tbxCurrentDomain.Text, _securityLevel);

        if (ViewState["id"] != null)
        {
            int id = 0;
            if (int.TryParse(ViewState["id"].ToString(), out id))
            {
                if (Sitesettings.Save(
				id, 
				settingName,
				adminEmail,
				securityKey,
				supportEmail,
				smtpHost,
				smtpUsername,
				smtpPassword,
				smtpPort,
				smtpUseSsl,
				siteName,
				defaultTheme,
				defaultTimeZone,
				defaultUserroleId,
				currentDomain
                ))
                    Response.Redirect("sitesettingslist.aspx");
                else
                    ResponseMessage = "Save failed";
            }
            else
                ResponseMessage = "Save failed";
        }
        else
        {
            Sitesettings sitesettings = Sitesettings.Create(
				settingName,
				adminEmail,
				securityKey,
				supportEmail,
				smtpHost,
				smtpUsername,
				smtpPassword,
				smtpPort,
				smtpUseSsl,
				siteName,
				defaultTheme,
				defaultTimeZone,
				defaultUserroleId,
				currentDomain
                );

            if (sitesettings != null)
                Response.Redirect("sitesettingslist.aspx");
            else
                ResponseMessage = "Failed to create user";
        }
    }

    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("sitesettingslist.aspx");
    }
}
